﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<ModulePermissionViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <title>
        <%= this. Model.Localize("ControlPermissionsPageTitle")%>
    </title>
    <% Html.RenderCssFile("Template.css"); %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%= Html.SuccessNotification("successNotification", Model.Localize("SiteSettingsSuccessNotification"))%>
    <%= Html.ErrorNotification("errorNotification", Model.Localize("SiteSettingsErrorNotification"))%>
    <div class="page-title" id="control-permissions-page-header">
        <h1>
            <%= Model.Localize("ControlPermissionsPageHeader")%></h1>
    </div>
    <div class="page-menu">
        <%= Html.UrlActionLink(Model.Localize("BackLink"), Url.PageTemplate(Model.CtrId))%> |
        <%= Html.UrlActionLink(Model.Localize("SaveLink"), "javascript:void(0);", new { onclick = "Save();" })%>
    </div>
    <% using (Html.BeginForm(Url.PermissionsPageContent(Model.CtrId), new { id = "permissionsForm" }))
       { %>
    <table id="module-permissions" class="grid">
        <thead>
            <tr>
                <th>
                    Role
                </th>
                <%foreach (var permission in Model.Permissions)
                  {
                %>
                <th>
                    <%= permission.Name %>
                </th>
                <%
                    } %>
            </tr>
        </thead>
        <%   int i = 0;
             foreach (var item in Model.Roles)
             {
        %>
        <tbody>
            <tr>
                <td>
                    <%= item.Name %>
                </td>
                <%
                    foreach (var permission in Model.Permissions)
                    {
                %>
                <td>
                    <%if (Model.ControlPermissions.Any(m => m.PermissionId == permission.Id && m.RoleId == item.Id))
                      {
                    %>
                    <%= Html.Hidden("permissions[" + i.ToString() + "].Id", permission.Id)%>
                    <%= Html.Hidden("permissions[" + i.ToString() + "].RoleId", item.Id)%>
                    <%= Html.CheckBox("permissions[" + i.ToString() + "].Selected", true)%>
                    <%
                        }
                      else
                      {%>
                    <%= Html.Hidden("permissions[" + i.ToString() + "].Id", permission.Id)%>
                    <%= Html.Hidden("permissions[" + i.ToString() + "].RoleId", item.Id)%>
                    <%= Html.CheckBox("permissions[" + i.ToString() + "].Selected", false)%>
                    <%} %>
                </td>
                <%i++;
                    } %>
            </tr>
        </tbody>
        <%
            } %>
        <tfoot>
            <tr>
                <td colspan="<%= Model.Permissions.Count() + 1 %>">
                </td>
            </tr>
        </tfoot>
    </table>
    <% } %>

    <script type="text/javascript">

        function Save() {
            var options = {
                success: function(data) {
                    var json = JSON.parse(data);
                    $('.notification').hide('slow');
                    if (json.model == "invalid") {
                        $('#errorNotification').show('fast');
                        var result = "<ul>";
                        for (var i in json.errors) {
                            result += "<li>" + json.errors[i] + "</li>";
                        }
                        result += "</ul>";
                        $('#errorNotification .notification-msg-content').html(result);
                    }
                    else {
                        $('#successNotification').show('fast');
                    }
                },
                error: function(request, textStatus, errorThrown) {
                    $('.notification').hide('fast');
                    $('#errorNotification').show('fast');
                }
            };
            $('#permissionsForm').ajaxSubmit(options);
        }
    
    </script>

</asp:Content>
